
package cn.edu.scau.cmi.hombio.dao;

import cn.edu.scau.cmi.hombio.domain.Sale;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Sale entities.
 * 
 */
@Repository("SaleDAO")

@Transactional
public class SaleDAOImpl extends AbstractJpaDao<Sale> implements SaleDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Sale.class }));

	/**
	 * EntityManager injected by Spring for persistence unit pig
	 *
	 */
	@PersistenceContext(unitName = "pig")
	private EntityManager entityManager;

	/**
	 * Instantiates a new SaleDAOImpl
	 *
	 */
	public SaleDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findSaleById
	 *
	 */
	@Transactional
	public Sale findSaleById(Integer id) throws DataAccessException {

		return findSaleById(id, -1, -1);
	}

	/**
	 * JPQL Query - findSaleById
	 *
	 */

	@Transactional
	public Sale findSaleById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findSaleById", startResult, maxRows, id);
			return (cn.edu.scau.cmi.hombio.domain.Sale) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findSaleByMemo
	 *
	 */
	@Transactional
	public Set<Sale> findSaleByMemo(String memo) throws DataAccessException {

		return findSaleByMemo(memo, -1, -1);
	}

	/**
	 * JPQL Query - findSaleByMemo
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Sale> findSaleByMemo(String memo, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSaleByMemo", startResult, maxRows, memo);
		return new LinkedHashSet<Sale>(query.getResultList());
	}

	/**
	 * JPQL Query - findAllSales
	 *
	 */
	@Transactional
	public Set<Sale> findAllSales() throws DataAccessException {

		return findAllSales(-1, -1);
	}

	/**
	 * JPQL Query - findAllSales
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Sale> findAllSales(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllSales", startResult, maxRows);
		return new LinkedHashSet<Sale>(query.getResultList());
	}

	/**
	 * JPQL Query - findSaleByMemoContaining
	 *
	 */
	@Transactional
	public Set<Sale> findSaleByMemoContaining(String memo) throws DataAccessException {

		return findSaleByMemoContaining(memo, -1, -1);
	}

	/**
	 * JPQL Query - findSaleByMemoContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Sale> findSaleByMemoContaining(String memo, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSaleByMemoContaining", startResult, maxRows, memo);
		return new LinkedHashSet<Sale>(query.getResultList());
	}

	/**
	 * JPQL Query - findSaleBySaleType
	 *
	 */
	@Transactional
	public Set<Sale> findSaleBySaleType(String saleType) throws DataAccessException {

		return findSaleBySaleType(saleType, -1, -1);
	}

	/**
	 * JPQL Query - findSaleBySaleType
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Sale> findSaleBySaleType(String saleType, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSaleBySaleType", startResult, maxRows, saleType);
		return new LinkedHashSet<Sale>(query.getResultList());
	}

	/**
	 * JPQL Query - findSaleByPrimaryKey
	 *
	 */
	@Transactional
	public Sale findSaleByPrimaryKey(Integer id) throws DataAccessException {

		return findSaleByPrimaryKey(id, -1, -1);
	}

	/**
	 * JPQL Query - findSaleByPrimaryKey
	 *
	 */

	@Transactional
	public Sale findSaleByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findSaleByPrimaryKey", startResult, maxRows, id);
			return (cn.edu.scau.cmi.hombio.domain.Sale) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findSaleBySaleTypeContaining
	 *
	 */
	@Transactional
	public Set<Sale> findSaleBySaleTypeContaining(String saleType) throws DataAccessException {

		return findSaleBySaleTypeContaining(saleType, -1, -1);
	}

	/**
	 * JPQL Query - findSaleBySaleTypeContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Sale> findSaleBySaleTypeContaining(String saleType, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSaleBySaleTypeContaining", startResult, maxRows, saleType);
		return new LinkedHashSet<Sale>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Sale entity) {
		return true;
	}
}
